home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gamtlk11.zip / source.zip / CARIBBEAN POKER / CPOKER.CPP < prev    next >
C/C++ Source or Header  |  1999-12-13  |  13KB  |  425 lines

  1. //
  2. // File: CPoker.CPP
  3. //
  4. // Main source file for the Caribbean Poker Game
  5. //
  6. #include <icoordsy.hpp>
  7. #include <time.h>
  8. #include "CPoker.HPP"
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.    ICoordinateSystem::setApplicationOrientation(ICoordinateSystem::kOriginLowerLeft);
  13.    IStaticText::setDefaultStyle(IStaticText::classDefaultStyle & ~(IStaticText::left | IStaticText::top) | IStaticText::center | IStaticText::vertCenter);
  14.    CPOKER   CPoker(MainWindow);
  15.  
  16.    try
  17.    {
  18.       CPoker.setFocus();
  19.       CPoker.show();
  20.       IApplication::current().run();
  21.    } catch(...)
  22.    {
  23.       IMessageBox    Idiot(0);
  24.       Idiot.show("Cannot run");
  25.    };
  26.    return 0;
  27. }
  28.  
  29. //
  30. // CPOKER::CPOKER(unsigned long WindowID)
  31. //
  32. // Main constructor for the CPOKER window
  33. //
  34. CPOKER::CPOKER(unsigned long WindowID) : IFrameWindow(IFrameWindow::defaultStyle() | IFrameWindow::minimizedIcon | IFrameWindow::animated | IFrameWindow::windowList | IFrameWindow::menuBar, WindowID),
  35.    drawCanvas(MainClient, this, this),
  36.    Profile(),
  37.    betBitmap(IResourceLibrary().loadBitmap(BMP_BET)),
  38.    emptyBitmap(IResourceLibrary().loadBitmap(BMP_EMPTY)),
  39.    anteBitmap(IResourceLibrary().loadBitmap(BMP_ANTE)),
  40.    progfullBitmap(IResourceLibrary().loadBitmap(BMP_PROGFULL)),
  41.    progemptyBitmap(IResourceLibrary().loadBitmap(BMP_PROGEMPTY)),
  42.    betButton(PB_BET, this, this, IRectangle(0,0), IPushButton::defaultStyle() | IWindow::disabled),
  43.    foldButton(PB_FOLD, this, this, IRectangle(0,0), IPushButton::defaultStyle() | IWindow::disabled),
  44.    progressiveButton(PB_PROGRESSIVE, this, this, BMP_PROGEMPTY), betField(PB_BETFIELD, this, this, BMP_EMPTY),
  45.    autoButton(PB_AUTO, this, this), wager(SPBN_WAGER, this, this),
  46.    PlayerHand(this, PLAYER_HAND), DealerHand(this, DEALER_HAND),
  47.    bank(TXT_BANK, this, this),
  48.    jackpot(TXT_JACKPOT, this, this),
  49.    payout(TXT_PAYOUT, this, this),
  50.    ivoryChip(false),
  51.    State(DONE),
  52.    commandHandler(this),
  53.    resizeHandler(this)
  54. {
  55.    srand(time(NULL));
  56.    betButton.setText(PMT_BET);
  57.    foldButton.setText(PMT_FOLD);
  58.    autoButton.setText(PMT_AUTO);
  59.    autoButton.select(Profile.isAuto());
  60.    wager.setRange(IRange(MIN_WAGER, MAX_WAGER));
  61.    wager.setValue(Profile.Wager);
  62.    wager.setForegroundColor(IColor(IColor::kBlack));
  63.    setClient(&drawCanvas);
  64.    selectGame(Profile.game());
  65.    setForegroundColor(Profile.Foreground);
  66.    setBackgroundColor(Profile.Background);
  67.    commandHandler.handleEventsFor(&drawCanvas);
  68.    resizeHandler.handleEventsFor(&drawCanvas);
  69.    sizeTo(Profile.Size);
  70.    moveTo(Profile.Position);
  71.    animationTimer.start(new ANIMATE(this), Profile.Speed);
  72.    updateBank();
  73. }
  74.  
  75. //
  76. // CPOKER::~CPOKER()
  77. //
  78. // Main destructor for CPOKER
  79. //
  80. CPOKER::~CPOKER()
  81. {
  82.    commandHandler.stopHandlingEventsFor(this);
  83. }
  84.  
  85. //
  86. // bool CPOKER::sizeClient(IResizeEvent& resizeEvent)
  87. //
  88. // Called whenever the client window requires resizing
  89. //
  90. bool CPOKER::sizeClient(IResizeEvent& resizeEvent)
  91. {
  92.    enableUpdate(false);
  93.  
  94.    betButton.moveTo(IPoint(BET_XPOS*resizeEvent.newSize().width(), BET_YPOS*resizeEvent.newSize().height()));
  95.    betButton.sizeTo(ISize(BET_XSIZE*resizeEvent.newSize().width(), BET_YSIZE*resizeEvent.newSize().height()));
  96.    foldButton.moveTo(IPoint(FOLD_XPOS*resizeEvent.newSize().width(), FOLD_YPOS*resizeEvent.newSize().height()));
  97.    foldButton.sizeTo(ISize(FOLD_XSIZE*resizeEvent.newSize().width(), FOLD_YSIZE*resizeEvent.newSize().height()));
  98.    progressiveButton.moveTo(IPoint(PROG_XPOS*resizeEvent.newSize().width(), PROG_YPOS*resizeEvent.newSize().height()));
  99.    progressiveButton.sizeTo(ISize(PROG_XSIZE*resizeEvent.newSize().width(), PROG_YSIZE*resizeEvent.newSize().height()));
  100.    autoButton.moveTo(IPoint(AUTO_XPOS*resizeEvent.newSize().width(), AUTO_YPOS*resizeEvent.newSize().height()));
  101.    autoButton.sizeTo(ISize(AUTO_XSIZE*resizeEvent.newSize().width(), AUTO_YSIZE*resizeEvent.newSize().height()));
  102.    betField.moveTo(IPoint(BETFIELD_XPOS*resizeEvent.newSize().width(), BETFIELD_YPOS*resizeEvent.newSize().height()));
  103.    betField.sizeTo(ISize(BETFIELD_XSIZE*resizeEvent.newSize().width(), BETFIELD_YSIZE*resizeEvent.newSize().height()));
  104.    wager.moveTo(IPoint(WAGER_XPOS*resizeEvent.newSize().width(), WAGER_YPOS*resizeEvent.newSize().height()));
  105.    wager.sizeTo(ISize(WAGER_XSIZE*resizeEvent.newSize().width(), wager.size().height()));
  106.    PlayerHand.resize(ISize(PHAND_XSIZE*resizeEvent.newSize().width(), PHAND_YSIZE*resizeEvent.newSize().height()),
  107.       IPoint(PHAND_XPOS*resizeEvent.newSize().width(), PHAND_YPOS*resizeEvent.newSize().height()));
  108.    DealerHand.resize(ISize(DHAND_XSIZE*resizeEvent.newSize().width(), DHAND_YSIZE*resizeEvent.newSize().height()),
  109.       IPoint(DHAND_XPOS*resizeEvent.newSize().width(), DHAND_YPOS*resizeEvent.newSize().height()));
  110.    bank.moveTo(IPoint(BANK_XPOS*resizeEvent.newSize().width(), BANK_YPOS*resizeEvent.newSize().height()));
  111.    bank.sizeTo(ISize(BANK_XSIZE*resizeEvent.newSize().width(), BANK_YSIZE*resizeEvent.newSize().height()));
  112.    jackpot.moveTo(IPoint(JACKPOT_XPOS*resizeEvent.newSize().width(), JACKPOT_YPOS*resizeEvent.newSize().height()));
  113.    jackpot.sizeTo(ISize(JACKPOT_XSIZE*resizeEvent.newSize().width(), JACKPOT_YSIZE*resizeEvent.newSize().height()));
  114.    payout.moveTo(IPoint(PAYOUT_XPOS*resizeEvent.newSize().width(), PAYOUT_YPOS*resizeEvent.newSize().height()));
  115.    payout.sizeTo(ISize(PAYOUT_XSIZE*resizeEvent.newSize().width(), PAYOUT_YSIZE*resizeEvent.newSize().height()));
  116.    enableUpdate(true);
  117.  
  118.    return true;
  119. }
  120.  
  121. //
  122. // bool CPOKER::deal()
  123. //
  124. // Deals a new hand
  125. //
  126. bool CPOKER::deal()
  127. {
  128.    float adamBank=Profile.Bank;
  129.  
  130.    // Verify that the hand may begin
  131.    Profile.Wager=wager.value();
  132.    adamBank-=Profile.Wager;
  133.    if(!ivoryChip && autoButton.isSelected())
  134.       if(addIvory())
  135.          adamBank-=1;
  136.  
  137.    if(adamBank<0) return false;
  138.  
  139.    State=DEAL;
  140.    payout.setText("");
  141.    Profile.Bank=adamBank;
  142.    betField.setGraphic(anteBitmap);
  143.    updateBank();
  144.    disable();
  145.    wager.disable();
  146.    foldButton.enable();
  147.    betButton.enable();
  148.    MainDeck.shuffle();
  149.    enableUpdate(false);
  150.    DealerHand.erase();
  151.    PlayerHand.erase();
  152.    enableUpdate(true);
  153.  
  154.    return true;
  155. }
  156.  
  157. //
  158. // bool CPOKER::bet()
  159. //
  160. // Places a bet and continues the game
  161. //
  162. bool CPOKER::bet()
  163. {
  164.    if(Profile.Bank<2*Profile.Wager || State!=DEAL) return false;
  165.  
  166.    State=DRAW;
  167.    Profile.Bank-=2*Profile.Wager;
  168. //   betField.setGraphic(BMP_BET);
  169.    betField.setGraphic(betBitmap);
  170.    PlayerHand.discard(PlayerHand.marked());
  171.    updateBank();
  172.    return true;
  173. }
  174.  
  175. //
  176. // bool CPOKER::fold()
  177. //
  178. // Called when the player chooses to fold
  179. //
  180. bool CPOKER::fold()
  181. {
  182.    if(State!=DEAL) return false;
  183.  
  184.    State=FOLD;
  185.    betField.setGraphic(emptyBitmap);
  186.    PlayerHand.fold();
  187.    disable();
  188.    return true;
  189. }
  190.  
  191. //
  192. // bool CPOKER::selectGame(unsigned long Game)
  193. //
  194. // Selects game from menu
  195. //
  196. bool CPOKER::selectGame(unsigned long Game)
  197. {
  198.    if(State!=DONE) return false;
  199.  
  200.    if(Game==IDM_DRAW)
  201.    {
  202.       Profile.Preferences|=PROFILE::CARIBBEAN_DRAW;
  203.       IMenuBar(this, IMenuBar::wrapper).checkItem(IDM_STUD, false);
  204.       IMenuBar(this, IMenuBar::wrapper).checkItem(IDM_DRAW, true);
  205.       IStaticText(IFrameWindow::handleFor(IFrameWindow::titleBar)).setText(CPOKER_PMT_CARIBBEANDRAW);
  206.    } else
  207.    {
  208.       Profile.Preferences&=~PROFILE::CARIBBEAN_DRAW;
  209.       IMenuBar(this, IMenuBar::wrapper).checkItem(IDM_DRAW, false);
  210.       IMenuBar(this, IMenuBar::wrapper).checkItem(IDM_STUD, true);
  211.       IStaticText(IFrameWindow::handleFor(IFrameWindow::titleBar)).setText(CPOKER_PMT_CARIBBEANSTUD);
  212.    }
  213.    refresh();
  214.    return true;
  215. }
  216.  
  217. //
  218. // bool CPOKER::updateBank()
  219. //
  220. // Updates the bank display based on the bank value stored in the profile.
  221. //
  222. bool CPOKER::updateBank()
  223. {
  224.    bank.setText(IResourceLibrary().loadString(PMT_BANK)+IString(Profile.Bank));
  225.  
  226.    return true;
  227. }
  228.  
  229. //
  230. // bool CPOKER::updateJackpot()
  231. //
  232. // Updates the jackpot display
  233. //
  234. bool CPOKER::updateJackpot()
  235. {
  236.    jackpot.setText(IResourceLibrary().loadString(PMT_JACKPOT)+IString((unsigned long)Profile.Jackpot));
  237.  
  238.    return true;
  239. }
  240.  
  241. //
  242. // bool CPOKER::addIvory()
  243. //
  244. // Adds a $1 progressive chip to the acceptor, if the bank has at least $1.
  245. //
  246. bool CPOKER::addIvory()
  247. {
  248.    if(Profile.Bank>=1)
  249.    {
  250.       Profile.Bank-=1;
  251.       updateBank();
  252. //      progressiveButton.setGraphic(BMP_PROGFULL);
  253.       progressiveButton.setGraphic(progfullBitmap);
  254.       ivoryChip=true;
  255.    } else return false;
  256.    return true;
  257. }
  258.  
  259. //
  260. // bool CPOKER::removeIvory()
  261. //
  262. // Removes a $1 chip from the acceptor
  263. //
  264. bool CPOKER::removeIvory()
  265. {
  266.    Profile.Bank+=1;
  267.    updateBank();
  268.    return takeIvory();
  269.    return true;
  270. }
  271.  
  272. //
  273. // bool CPOKER::takeIvory()
  274. //
  275. // Removes a $1 chip from the acceptor but does NOT credit the player
  276. //
  277. bool CPOKER::takeIvory()
  278. {
  279. //   progressiveButton.setGraphic(BMP_PROGEMPTY);
  280.    progressiveButton.setGraphic(progemptyBitmap);
  281.    ivoryChip=false;
  282.    return true;
  283. }
  284.  
  285. //
  286. // bool CPOKER::clockTick()
  287. //
  288. // Called whenever the animation timer ticks
  289. //
  290. bool CPOKER::clockTick()
  291. {
  292.    // Unconditionally update the jackpot
  293.    Profile.Jackpot+=JACKPOT_INCREMENT;
  294.    updateJackpot();
  295.  
  296.    switch(State) {
  297.       case DONE:
  298.          break;
  299.       case DEAL:
  300.          // Deal initial hand (Assume all money has changed hands)
  301.          if(PlayerHand.Size()<HANDSIZE)
  302.             PlayerHand.addCard(MainDeck.draw());
  303.          else if(!DealerHand.Size())
  304.          {
  305.             PlayerHand.evaluate();
  306.             DealerHand.addCard(MainDeck.draw());
  307.             enable();
  308.             payJackpot();
  309.          }
  310.          break;
  311.       case DRAW:
  312.       case FOLD:
  313.          // Deal any remaining cards to the player
  314.          if(PlayerHand.Size()<HANDSIZE && State!=FOLD)
  315.             PlayerHand.addCard(MainDeck.draw());
  316.          else
  317.          {
  318.             if(DealerHand.Size()==1 && State!=FOLD)
  319.                PlayerHand.evaluate();
  320.  
  321.             if(DealerHand.Size()<HANDSIZE)
  322.             {
  323.                DealerHand.addCard(MainDeck.draw());
  324.                if(DealerHand.Size()==HANDSIZE)
  325.                {
  326.                   DealerHand.evaluate();
  327.                   State=FOLD+1;
  328.                }
  329.             }
  330.          }
  331.          break;
  332.       case DMARK:
  333.          if(Profile.game()==IDM_STUD)
  334.             State=DDRAW;
  335.          else
  336.          {
  337.             DealerHand.discard(DealerHand.value() & 0x000000FF);
  338.             State++;
  339.          }
  340.          break;
  341.       case DDRAW:
  342.          if(DealerHand.Size()<HANDSIZE)
  343.             DealerHand.addCard(MainDeck.draw());
  344.          else
  345.          {
  346.             DealerHand.evaluate();
  347.             enable();
  348.             if(PlayerHand.value())
  349.                payPlayer();
  350.             takeIvory();
  351.             wager.enable();
  352.             State=DONE;
  353.          }
  354.          break;
  355.       default:
  356.          State++;
  357.          break;
  358.    }
  359.    return true;
  360. }
  361.  
  362. //
  363. // float CPOKER::payPlayer()
  364. //
  365. // Pays the player, if appropriate
  366. //
  367. float CPOKER::payPlayer()
  368. {
  369.    const int StudPayout[]={1,1,1,2,3,4,5,7,25,50,100};
  370.    const int DrawPayout[]={1,1,1,1,2,3,5,7,25,50,100};
  371.  
  372.    if(DealerHand.value()>>28<HAND::ACE_KING ||
  373.       (Profile.game()==IDM_DRAW && (DealerHand.value()>>24)<((HAND::PAIR<<4) | CARD::EIGHT)))
  374.       {
  375.          payout.setText(IResourceLibrary().loadString(PMT_PAYOUT)+IString(Profile.Wager*4));
  376.          Profile.Bank+=Profile.Wager*4;
  377.       }
  378.    else if(PlayerHand>DealerHand)
  379.    {
  380.       payout.setText(IResourceLibrary().loadString(PMT_PAYOUT)+IString(Profile.Wager*(4+2*(Profile.game()==IDM_STUD?StudPayout[PlayerHand.value()>>28]:DrawPayout[PlayerHand.value()>>28]))));
  381.       Profile.Bank+=Profile.Wager*(4+2*(Profile.game()==IDM_STUD?StudPayout[PlayerHand.value()>>28]:DrawPayout[PlayerHand.value()>>28]));
  382.    }
  383.    else if(DealerHand==PlayerHand)
  384.    {
  385.       payout.setText(IResourceLibrary().loadString(PMT_PAYOUT)+IString(Profile.Wager*3));
  386.       Profile.Bank+=3*Profile.Wager;
  387.    } else payout.setText("");
  388.  
  389.    updateBank();
  390.  
  391.    betField.setGraphic(emptyBitmap);
  392.    return 0;
  393. }
  394.  
  395. //
  396. // float CPOKER::payJackpot()
  397. //
  398. // Pays jackpot based on initial hand value
  399. //
  400. float CPOKER::payJackpot()
  401. {
  402.    const double   JackpotPayouts[]={0,0,0,0,0,0,50,100,500,-0.1,-1}, RetVal=JackpotPayouts[PlayerHand.value()>>28];
  403.  
  404.    if(RetVal)
  405.    {
  406.       if(RetVal>0)
  407.       {
  408.          payout.setText(IResourceLibrary().loadString(PMT_PROGPAYOUT)+IString(RetVal));
  409.          Profile.Jackpot-=RetVal;
  410.          Profile.Bank+=RetVal;
  411.       } else
  412.       {
  413.          payout.setText(IResourceLibrary().loadString(PMT_PROGPAYOUT)+IString(RetVal*Profile.Jackpot));
  414.          Profile.Bank-=RetVal*Profile.Jackpot;
  415.          Profile.Jackpot+=RetVal*Profile.Jackpot;
  416.       }
  417.       if(Profile.Jackpot<DEF_JACKPOT)
  418.          Profile.Jackpot=DEF_JACKPOT;
  419.  
  420.       updateBank();
  421.       updateJackpot();
  422.    } else payout.setText("");
  423.  
  424.    return RetVal;
  425. }